﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace shopform
{
    public partial class CompanyIDSelectorForm : Form
    {
        private String selectedID;

        public void initLV()
        {
            try
            {
                DataTable s;
                if (this.TBCompanyName.Text.Length == 0)
                {
                    s = Company.getAllCompaniesForSelection();
                }
                else
                {
                    s = Company.getAllCompaniesWithNameLike(this.TBCompanyName.Text);   
                }

                this.DGVCompanies.DataSource = s;
                String[] labels = { 
                                    "Cegnev", "Unios adoszam", "Kereksedelmi szam",
                                    "Orszag", "Varos", "Nyelv"
                                  };

                for (int i = 0; i < this.DGVCompanies.Columns.Count; ++i)
                {
                    this.DGVCompanies.Columns[i].HeaderCell.Value = labels[i];
                }

                this.DGVCompanies.AutoResizeColumns();
                this.DGVCompanies.AutoSizeColumnsMode =
                        DataGridViewAutoSizeColumnsMode.AllCells;
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        public CompanyIDSelectorForm()
        {
            InitializeComponent();
            initLV();
        }

        public String getSelectedCompanyID()
        {
            return this.selectedID;
        }

        private void ButtonCancel_Click(object sender, EventArgs e)
        {
            this.selectedID = "none";
            this.Close();
        }

        private void ButtonChoose_Click(object sender, EventArgs e)
        {
            if (this.DGVCompanies.Rows.Count == 0)
            {
                MessageBox.Show("Nincs kivalasztva semmi!\n" +
                                "Szukits a filtereken vagy lepj vissza!");
                return;
            }
            this.selectedID = 
                this.DGVCompanies.SelectedRows[0].Cells[1].Value.ToString();
            this.Close();
        }

        private void TBCompanyName_TextChanged(object sender, EventArgs e)
        {
            this.initLV();
        }

    }
}
